package com.zxmr.utility.audit;

import java.util.Calendar;

import com.zxmr.asyn.TaskMonitor;
import com.zxmr.audit.AuditScheduleCallBack;
import com.zxmr.util.Utils;

public class TaskStateAuditManager {

	public static void init(TaskMonitor monitor)
	{
		// setup four audit instance in different time

		TaskStateAudit first = newAudit(0,0,monitor);
		TaskStateAudit second = newAudit(8,0,monitor);
		TaskStateAudit third = newAudit(12,0,monitor);
		TaskStateAudit forth = newAudit(18,0,monitor);
		
		first.setNextAudit(second);
		second.setNextAudit(third);
		third.setNextAudit(forth);
		forth.setNextAudit(first);
		
		TaskStateAudit needInsert = first;
		if(first.getAuditTime() < 0)
		{
			if(second.getAuditTime() >0)
			{
				needInsert = second;
			}else if(third.getAuditTime() > 0)
				{
					needInsert = third;
				}else if(forth.getAuditTime() > 0)
				{
					needInsert = forth;
				}else
				{
					needInsert = first;
					first.getCalendar().add(Calendar.DAY_OF_MONTH, 1);
				}
		}
		System.out.println("final calendar is "+needInsert.getCalendar().get(Calendar.HOUR_OF_DAY));	
		insertAudit(needInsert,monitor);

	}
	private static void insertAudit(TaskStateAudit audit,TaskMonitor monitor)
	{
		AuditScheduleCallBack callback = new AuditScheduleCallBack(audit);
		monitor.addTask(callback, audit.getAuditTime());
	}
	private static TaskStateAudit newAudit(int hour,int minu,TaskMonitor monitor)
	{
		Calendar fistCal = Utils.getResetCalendar();
		fistCal.set(Calendar.HOUR_OF_DAY, hour);
		fistCal.set(Calendar.MINUTE, minu);
		TaskStateAudit first = new TaskStateAudit(fistCal,monitor);
		return first;
	}
}
